#pragma once

#include <ExDisp.h>
#include <ExDispid.h>

class CTestDialog;
typedef IDispEventImpl<IDC_EXPLORER11, CTestDialog, &DIID_DWebBrowserEvents2, &LIBID_SHDocVw, 1, 0> MSIEEvent2Impl;

class CXeAxWindow;
class CTestDialog 
	: public CDialogImpl<CTestDialog>
	, MSIEEvent2Impl
	
{
public:
	enum { IDD = IDD_DIALOG1};

public:
	CTestDialog(void);
	~CTestDialog(void);

	BEGIN_MSG_MAP(CTestDialog)
		MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
		MESSAGE_HANDLER(WM_CLOSE, OnClose)
		MESSAGE_HANDLER(WM_DESTROY, OnDestroy)
	END_MSG_MAP()

	BEGIN_SINK_MAP(CTestDialog)
		SINK_ENTRY_EX(IDC_EXPLORER11, DIID_DWebBrowserEvents2, DISPID_BEFORENAVIGATE2, OnBeforeNavigate2)
		SINK_ENTRY_EX(IDC_EXPLORER11, DIID_DWebBrowserEvents2, DISPID_NEWWINDOW3, OnNewWindow3)
	END_SINK_MAP()

public:
	LRESULT OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
	LRESULT OnClose(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
	LRESULT OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);

	void STDMETHODCALLTYPE OnBeforeNavigate2(IDispatch *pDisp,
		VARIANT *pvURL,
		VARIANT *pvtFlags,
		VARIANT *pvtTargetFrameName,
		VARIANT *pvtPostData,
		VARIANT *pvtHeaders, 
		VARIANT_BOOL *pvbCancel);

	void STDMETHODCALLTYPE OnNewWindow3(IDispatch **ppDisp,
		VARIANT_BOOL *pvbCancel,
		DWORD dwFlags,
		BSTR bstrURLContext,
		BSTR bstrURL);

private:
	bool isFromWebBrowser(IDispatch *pDisp);

private:
	CXeAxWindow *m_pAxWindow;
	CComPtr<IWebBrowser2> m_smpBrowser2;
};